package com.yc.web.servlets;

import com.yc.ResfoodBiz;
import com.yc.bean.Resfood;
import com.yc.bean.Resuser;
import com.yc.commons.DBHelper;
import com.yc.commons.RedisHelper;
import com.yc.model.CartItem;
import com.yc.model.JsonModel;
import com.yc.model.PageBean;
import com.yc.utils.YcConstants;
import redis.clients.jedis.Jedis;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zwei
 * @create 2024-06-12 19:54
 */
@WebServlet("/food.action")
public class ResfoodServlet extends BaseServlet{
    protected void findFoodsPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JsonModel jm=new JsonModel();
        PageBean pageBean=null;
        try {
            pageBean=super.parseObjectFromRequest(req,PageBean.class);
            //调用业务层完成分页查询
            ResfoodBiz resfoodBiz=new ResfoodBiz();
            pageBean=resfoodBiz.findByPage(pageBean);
            HttpSession session=req.getSession();
            session.setAttribute("foodlist",pageBean.getDataset());
            jm.setCode(1);
            jm.setObj(pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jm.setCode(0);
            jm.setError("分页查询失败");
        }
        super.writeJson(jm,resp);
    }
    protected void findAllFoods(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        JsonModel jm=new JsonModel();
        String sql="select*from resfood";
        DBHelper dbHelper=new DBHelper();
        List<Resfood>list=dbHelper.select(Resfood.class,sql);
        //tomcat提供的缓存  =》将来上线当数据很多的时候 存到redis中
        HttpSession session=req.getSession();
        session.setAttribute("foodList",list);
        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm,resp);
    }
    protected void traceBroweFood(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JsonModel jm=new JsonModel();
        String fid=req.getParameter("fid");
        HttpSession session=req.getSession();
        if (session.getAttribute(YcConstants.RESUSER)==null){
            jm.setCode(0);
            jm.setError("未登录用户不记录足迹");
            super.writeJson(jm,resp);
        }
        Date date=new Date();
        Resuser resuser= (Resuser) session.getAttribute(YcConstants.RESUSER);
        int userid=resuser.getUserid();
        Jedis jedis=RedisHelper.getRedisInstance();
        //先检查  此键的长度  如果超过4个  则删除4以后的数据
        jedis.zadd(userid+YcConstants.REDIS_VISITED,date.getTime(),fid);
        long length=jedis.zcard(userid+YcConstants.REDIS_VISITED);
        if (length>4){
            //rem表示按降序排序
            jedis.zremrangeByRank(userid+YcConstants.REDIS_VISITED,0,0);
        }
        //在存

        System.out.println(length);
        jm.setCode(1);
        super.writeJson(jm,resp);

    }
    protected void getHistory(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        JsonModel jm=new JsonModel();
        HttpSession session=req.getSession();
        //权限  判断这一功能写到rightFilter中去
        Resuser resuser= (Resuser) session.getAttribute(YcConstants.RESUSER);
        int userid=resuser.getUserid();
        Jedis jedis= RedisHelper.getRedisInstance();
        Set<String>visitedFoods=jedis.zrevrange(userid+YcConstants.REDIS_VISITED,0,3);
        if (visitedFoods==null||visitedFoods.size()<=0){
            jm.setCode(0);
            super.writeJson(jm,resp);
            return;
        }
        //采用sql中的in拼接多个商品的查询  减少sql语句的操作
        //select*from resfood where fid in(?,?,?,?)
        StringBuffer sqlbuffer=new StringBuffer("select*from resfood where fid in ( ");
        for (String fid:visitedFoods){
            sqlbuffer.append("?,");
        }
        //select * from resfood where fid in(?,?,?,?,
        String sql=sqlbuffer.toString();
        //去掉多余的  ,
        sql=sql.substring(0,sql.length()-1);
        //select * from resfood where fid in(?,?,?,?
        sql=sql+")";
        //最终语句  //select * from resfood where fid in(?,?,?,?)
        //打印最终的sql语句
        System.out.println("最终执行的sql语句"+sql);
        List<Resfood>list=dbHelper.select(Resfood.class,sql,visitedFoods.toArray());
        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm,resp);
    }
    protected void clickPraise(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        JsonModel jm=new JsonModel();
        //取菜名
        Resfood resfood=null;
        resfood=super.parseObjectFromRequest(req,Resfood.class);
        int fid=resfood.getFid();
        HttpSession session=req.getSession();
        //权限这一功能  写到rightFilter中去
        Resuser resuser= (Resuser) session.getAttribute(YcConstants.RESUSER);
        int  userid=resuser.getUserid();
        Jedis jedis=RedisHelper.getRedisInstance();
        if (jedis.sismember(fid+YcConstants.REDIS_FOOD_PRAISE,userid+"")){
            //此用户已经点赞过这道菜  再次点击就是取消赞
            jedis.srem(fid+YcConstants.REDIS_FOOD_PRAISE,userid+"");
            //删除该用户对该道菜的点赞
            jedis.srem(userid+YcConstants.REDIS_PRAISE,fid+"");
        }else {
            //此用户没有对这道菜点赞
            jedis.sadd(fid+YcConstants.REDIS_FOOD_PRAISE,userid+"");
            //记录此用户对那些菜点过赞
            jedis.sadd(userid+YcConstants.REDIS_PRAISE,fid+"");
        }
        //取出最新点赞数
        long praise=jedis.scard(fid+YcConstants.REDIS_FOOD_PRAISE);
        resfood.setPraise(praise);
        jm.setCode(1);
        jm.setObj(resfood);
        super.writeJson(jm,resp);
    }
}
